package com.hutra.utils
{
	/**
	 * @author hunglx
	 */
	public class Dater{
		public function Dater(){}
		public static var unequal:Number = 0;
		public static var mid_unequal:Number = 0;
		public static var offset:Number = 0;
		public static var arrAVG:Array=new Array();
		public static function setOffset(timeclientsender:Number,timeserversender:Number):void
		{
			var thistime:Date = new Date  ;
			var ping:Number = thistime.time - timeclientsender;
			var latency:Number = ping / 2;
			unequal = timeserversender - thistime.time + latency;
			arrAVG.push(unequal);
			if (arrAVG.length >= 20)
			{
				var total:Number= new Number();
				for (var i:int=0; i<arrAVG.length; i++) total +=  arrAVG[i];
				unequal = Math.round(total/arrAVG.length);
				arrAVG = new Array();
			}
			if (mid_unequal == 0)
				{
					mid_unequal = unequal;
				}
				if (mid_unequal > unequal)
				{
					mid_unequal = unequal;
				}
				unequal = mid_unequal;
			offset = unequal;
		}
		public static function getOffset():Number
		{
			return offset;
		}
		public static function getTime():Number
		{
			var thistime:Date = new Date  ;
			return thistime.time + offset;
		}
		/**
		 * nếu thời gian lớn hơn 1 ngày thì thể hiện dạng ngày
		 * còn nếu thời gian nhỏ hơn 1 ngày thì thể hiện là giờ
		 * @param	value
		 * @param	isfull
		 * @return
		 */
		public function toTimeString(value:Number,isfull:Boolean=true):String{
            var datestring:String = "";
			var day:Number 		= Math.floor((value / 86400));
            var hour:Number 	= Math.floor((value - day * 86400) / 3600);
            var minute:Number 	= Math.floor((value - day * 86400 - hour * 3600) / 60);
            var second:Number 	= Math.floor((value - day * 86400 - hour * 3600 - minute * 60));
            var k:Number;
			datestring = datestring 
				+ ((day)?(String(day) + "d "):"")
				+ ((hour)?(String(hour) + "h "):"")
				+ ((minute)?(String(minute) + "m "):"")
				+ ((second)?(String(second) + "s "):"");
            return datestring;
        }
		public function humanReadableTime(obj1:Number) : String{
            var value2:* = Math.floor(obj1 / 1000);
            var value3:String = "";
            if (value2 < 0){
                value2 = Math.abs(value2);
                value3 = "-";
            }
            var value4:* = Math.floor(value2 / 60);
            if (Math.floor(value2 / 60) > 0){
                value2 = value2 - value4 * 60;
            }
            var value5:* = Math.floor(value4 / 60);
            if (Math.floor(value4 / 60) > 0){
                value4 = value4 - 60 * value5;
            }
            return value3 + (value5 < 10 ? ("0" + value5) : (value5)) + ":" + (value4 < 10 ? ("0" + value4) : (value4)) + ":" + (value2 < 10 ? ("0" + value2) : (value2));
        }
	}
}